home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / rdflib / sparql / sparqlGraph.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  3.9 KB  |  123 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from rdflib.Graph import Graph
  5.  
  6. class SPARQLGraph(Graph):
  7.     '''
  8.     A subclass of Graph with a few extra SPARQL bits.
  9.     '''
  10.     
  11.     def __init__(self, graph, graphVariable = None):
  12.         self.graphVariable = graphVariable
  13.         self.graph = graph
  14.         store = graph.store
  15.         identifier = graph.identifier
  16.         super(SPARQLGraph, self).__init__(store, identifier)
  17.  
  18.     
  19.     def _clusterForward(self, seed, Cluster):
  20.         '''Cluster the triple store: from a seed, transitively get all
  21.         properties and objects in direction of the arcs.
  22.  
  23.         @param seed: RDFLib Resource
  24.  
  25.         @param Cluster: a L{sparqlGraph} instance, that has to be
  26.         expanded with the new arcs
  27.         '''
  28.         
  29.         try:
  30.             for p, o in self.graph.predicate_objects(seed):
  31.                 if (seed, p, o) not in Cluster.graph:
  32.                     Cluster.add((seed, p, o))
  33.                     self._clusterForward(p, Cluster)
  34.                     self._clusterForward(o, Cluster)
  35.                     continue
  36.         except:
  37.             pass
  38.  
  39.  
  40.     
  41.     def clusterForward(self, seed, Cluster = None):
  42.         '''
  43.         Cluster the triple store: from a seed, transitively get all
  44.         properties and objects in direction of the arcs.
  45.  
  46.         @param seed: RDFLib Resource
  47.  
  48.         @param Cluster: another sparqlGraph instance; if None, a new
  49.         one will be created. The subgraph will be added to this graph.
  50.  
  51.         @returns: The triple store containing the cluster
  52.  
  53.         @rtype: L{sparqlGraph}
  54.         '''
  55.         if Cluster == None:
  56.             Cluster = SPARQLGraph()
  57.         
  58.         check_subject(seed)
  59.         self._clusterForward(seed, Cluster)
  60.         return Cluster
  61.  
  62.     
  63.     def _clusterBackward(self, seed, Cluster):
  64.         '''Cluster the triple store: from a seed, transitively get all
  65.         properties and objects in backward direction of the arcs.
  66.  
  67.         @param seed: RDFLib Resource
  68.  
  69.         @param Cluster: a L{sparqlGraph} instance, that has to be
  70.         expanded with the new arcs
  71.         '''
  72.         
  73.         try:
  74.             for s, p in self.graph.subject_predicates(seed):
  75.                 if (s, p, seed) not in Cluster.graph:
  76.                     Cluster.add((s, p, seed))
  77.                     self._clusterBackward(s, Cluster)
  78.                     self._clusterBackward(p, Cluster)
  79.                     continue
  80.         except:
  81.             pass
  82.  
  83.  
  84.     
  85.     def clusterBackward(self, seed, Cluster = None):
  86.         """
  87.         Cluster the triple store: from a seed, transitively get all
  88.         properties and objects 'backward', ie, following the link back
  89.         in the graph.
  90.  
  91.         @param seed: RDFLib Resource
  92.  
  93.         @param Cluster: another sparqlGraph instance; if None, a new
  94.         one will be created. The subgraph will be added to this graph.
  95.  
  96.         @returns: The triple store containing the cluster
  97.  
  98.         @rtype: L{sparqlGraph}
  99.         """
  100.         if Cluster == None:
  101.             Cluster = SPARQLGraph()
  102.         
  103.         check_object(seed)
  104.         self._clusterBackward(seed, Cluster)
  105.         return Cluster
  106.  
  107.     
  108.     def cluster(self, seed):
  109.         '''
  110.         Cluster up and down, by summing up the forward and backward
  111.         clustering
  112.  
  113.         @param seed: RDFLib Resource
  114.  
  115.         @returns: The triple store containing the cluster
  116.  
  117.         @rtype: L{sparqlGraph}
  118.         '''
  119.         raise 'Am I getting here?'
  120.         return self.clusterBackward(seed) + self.clusterForward(seed)
  121.  
  122.  
  123.